import { bindStore, addStore, editStore, deleteStore,getStore,activeStore } from '@/pages/api/index.js'
export default {
	data() {
		return {
			dialogVisible: false,
      labelPosition: 'top',
      isPrimary:'primary',   //控制激活按钮颜色
			formLabelAlign: {
				platform: '',
				shopName: '',
				shopOwner: '',
			},
			isEditShow: false,
			rules: {
				shopName: [
					{ required: true, message: '请输入店铺名称', trigger: 'blur' },
				],
				shopOwner: [
					{ required: true, message: '请输入旺旺号', trigger: 'blur' }
				],
				platform: [
					{ required: true, message: '请选择平台', trigger: 'change' }
				],
				address: [
					{ required: true, message: '请输入地址', trigger: 'blur' }
				],
			},
			options: [{
				value: '1',
				label: '淘宝'
			}, {
				value: '2',
				label: '天猫'
			}, {
				value: '3',
				label: '京东'
			}, {
				value: '4',
				label: '拼多多'
			}],
			params: {
				platform: 0,
				pageSize: 10,
				pageIndex: 1
			},
			total: 0,
			getList: [],
      addEdit: true,
      linkList:{
        userNick:'',
        userId:'',
      }
		}
	},
	methods: {
		init() {
			bindStore(this.params).then(res => {
				res.data.shop.forEach((item, index) => {
					if (item.platform == 1) {
						item.platform = '淘宝'
					} else if (item.platform == 2) {
						item.platform = '天猫'
					} else if (item.platform == 3) {
						item.platform = '京东'
					} else {
						item.platform = '拼多多'
					}
					switch (item.audit) {
						case 0:
							item.audit = '待激活'
							break
						case 1:
							item.audit = '已激活'
							break
					}
				})
				this.getList = res.data.shop
				this.total = res.data.count
			})
		},
		//点击让增加店铺弹框弹框显示
		addStore() {
			this.dialogVisible = true
			this.addEdit = true
			//复用弹框，点击增加数据，让input框的数据清空
			this.formLabelAlign = {
				platform: '',
				shopName: '',
				shopOwner: '',
			}
		},
		//点击弹框
		handleClose(done) {
			this.$confirm('确认关闭？')
				.then(_ => {
					done();
				})
				.catch(_ => { });
		},
		handleSizeChange(val) {
			console.log(`每页 ${val} 条`);
			this.params.pageSize = val
			this.init()
		},
		handleCurrentChange(val) {
			console.log(`当前页: ${val}`);
			this.params.pageIndex = val
			this.init()
		},
		//点击确定，添加店铺
		confirm(formName) {
      var reg = /^\s*$/g;
        if(reg.test(this.formLabelAlign.shopName)){
          this.$message.warning('店铺名称不能为空')
           return
        }
        if(reg.test(this.formLabelAlign.shopOwner)){
          this.$message.warning('旺旺号不能为空')
           return
        }
			this.$refs[formName].validate((valid) => {
				if (valid) {
					if (this.addEdit) {
            //正则去掉开头的0
            // this.formLabelAlign.shopName = this.formLabelAlign.shopName.replace(/\b(0+)/gi,"")
            // this.formLabelAlign.shopOwner = this.formLabelAlign.shopOwner.replace(/\b(0+)/gi,"")
						addStore(this.formLabelAlign).then(res => {
							if (res.code == 0) {
								this.$message({
									message: '添加店铺成功',
									type: "success"
								})
								this.dialogVisible = false
								this.init()
							} else {
								this.$message({
									message: res.msg,
									type: 'error'
								})
							}
						})
					} else {
            if(this.formLabelAlign.platform == '淘宝'){
              this.formLabelAlign.platform = 1
           }else if(this.formLabelAlign.platform == '天猫'){
              this.formLabelAlign.platform = 2
           }else if(this.formLabelAlign.platform == '京东'){
              this.formLabelAlign.platform = 3
           }else{
              this.formLabelAlign.platform = 4
           }
						editStore(this.formLabelAlign).then(res => {
							if (res.code == 0) {
								this.$message({
									message: '修改店铺成功',
									type: "success"
								})
								this.dialogVisible = false
								this.init()
							} else {
								this.$message({
									message: res.msg,
									type: 'error'
								})
							}
						})
					}
				} else {
					return false;
				}
			});
		},
		//编辑店铺
		handleEdit(row) {
      console.log(row,'row')
			this.dialogVisible = true
			this.addEdit = false

			this.formLabelAlign = {
				shopId: row.id,
				platform: row.platform,
				shopName: row.shop_name,
				shopOwner: row.shop_owner,
			}
			this.init()
		},
		//删除店铺
		handleDelete(row) {
			this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				deleteStore({
					shopId: row.id
				}).then(res => {
					if (res.code == 0) {
						this.init()
						this.$message({
							type: 'success',
							message: '删除成功!'
						})
					} else {
						this.$message({
							type: 'error',
							message: res.msg
						})
					}
				})

			}).catch(() => {
				this.$message({
					type: 'info',
					message: '已取消删除'
				});
			})
    },
    handleActive(index, row) {
      getStore({shopId:row.id}).then(res=>{
            if(res.code == 0){
                window.location.href = res.data.url
                this.init()
            }else{
               this.$message.error(res.msg)
            }
      })
    },
    /* 获取url参数 */
      getUrlParam (name) {
        var reg = new RegExp('(^|&)' + name + '=(.*)(&[^&=]+=)');
        var regLast = new RegExp('(^|&)' + name + '=(.*)($)');
        var r = window.location.search.substr(1).match(reg) || window.location.search.substr(1).match(regLast);
        if (r != null) {
          var l = r[2].match(/&[^&=]+=/)
          if (l) {
            return decodeURIComponent(r[2].split(l[0])[0]);
          } else return decodeURIComponent(r[2]);
        }
        return null;
    }
	},
	created() {
    this.linkList.userNick = this.getUrlParam('taobao_user_nick')
    this.linkList.userId = this.getUrlParam('taobao_user_id')
    console.log(this.linkList)
    if(this.linkList.userNick){
        activeStore(this.linkList).then(res=>{
            console.log(res,'res')
            if(res.code == 0){
              this.init()
            }else{
               this.$message.error(res.msg)
            }
        })
    }
    this.init()
	}
}
